import java.lang.reflect.GenericDeclaration;

public class BranchJump extends Quaternion {
    private Pairs pairs;
    private String label;       //begin end
    private String type;        //if while
    private int index;
    private int cond_index;

    public BranchJump(OP op, Pairs pairs) {
        super(op);
        this.pairs = pairs;
    }

    public BranchJump(OP op, String label, int index, String type) {
        super(op);
        this.label = label;
        this.type = type;
        this.index = index;
        this.cond_index = 0;
    }

    public void reversal() {
        if (getOp().equals(OP.BNE)) {
            this.setOp(OP.BEQ);
            this.label = this.label.equals("begin") ? "end" : "begin";
        } else {
            this.setOp(OP.BNE);
            this.label = this.label.equals("begin") ? "end" : "begin";
        }
    }

    public void toNextCond(int num) {
        if (getOp().equals(OP.BNE)) {
            //this.setOp(OP.BEQ);
            this.label = this.label.equals("begin") ? "end" : "begin";
            this.cond_index = num;
        } else {
            //this.setOp(OP.BNE);
            this.label = this.label.equals("begin") ? "end" : "begin";
            this.cond_index = num;
        }
    }

    public String getType() {
        return this.type;
    }

    public String getLabel() {
        return this.label;
    }

    public int getIndex() {
        return this.index;
    }


    public String getBranch() {
        if (this.cond_index > 0) {
            return  type + "_" + label + "_" + index + "_" + cond_index;
        }
        return this.type + "_" + this.label + "_" + index;
    }

    public String toString() {
        if (this.cond_index > 0) {
            return opToString() + " " + type + "_" + label + "_" + index + "_" + cond_index + "\n";
        }
        return opToString() + " " + type + "_" + label + "_" + index + "\n";
    }
}
